package cn.zj.logistics.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.zj.logistics.pojo.MessageObject;
import cn.zj.logistics.pojo.Permission;
import cn.zj.logistics.pojo.PermissionExample;
import cn.zj.logistics.pojo.PermissionExample.Criteria;
import cn.zj.logistics.service.PermissionService;

@Controller
@RequestMapping("/permission")
public class PermissionController {
	
	
	@Autowired
	private PermissionService permissionService;
	
	
	

	// 跳转到 permission 管理页面
	@RequiresPermissions("permission:permissionPage")
	@RequestMapping("/permissionPage")
	public String permissionPage() {
		return "permissionPage";
	}
	
	
	
	@ResponseBody
	@RequestMapping("/getAllPermission")
	public List<Permission> getAllPermission(){
		
		PermissionExample example = new PermissionExample();
		List<Permission> permissions = permissionService.selectByExample(example );
		
		return permissions;
	}
	
	
	
	@ResponseBody
	@RequiresPermissions("permission:update")
	@RequestMapping("/update")
	public MessageObject update(Permission permission) {
		
		
		int row = permissionService.updateByPrimaryKeySelective(permission);
		
		MessageObject mo = MessageObject.creatMO(0, "修改数据失败，请联系管理员");
		if(row == 1) {
			mo = MessageObject.creatMO(1, "修改数据成功");
		}
		
		return mo;
		
	}
	
	
	@ResponseBody
	@RequiresPermissions("permission:insert")
	@RequestMapping("/insert")
	public MessageObject insert(Permission permission) {
		
		int row = permissionService.insert(permission);
		
		MessageObject mo = MessageObject.creatMO(0, "新增数据失败，请联系管理员");
		if(row == 1) {
			mo = MessageObject.creatMO(1, "新增数据成功");
		}
		
		return mo;
		
	}
	
	
	/*
	 * jackson+@ResponseBody 方法直接返回json格式的数据
	 * 
	 */
	
	@ResponseBody
	@RequestMapping("/list")
	@RequiresPermissions("permission:list")
	public PageInfo<Permission> list(@RequestParam(defaultValue = "1")Integer pageNum,
								@RequestParam(defaultValue = "10")Integer pageSize,
								String keyword) {
		PageHelper.startPage(pageNum, pageSize);

		PermissionExample example = new PermissionExample();

		//开始条件查询
		if(StringUtils.isNotBlank(keyword)) {
			
			Criteria criteria1 = example.createCriteria();
			criteria1.andNameLike("%"+keyword+"%");
			
			
			
		}
		
		
		List<Permission> permissions = permissionService.selectByExample(example);

		PageInfo<Permission> pageInfo = new PageInfo<>(permissions);
		
		return pageInfo;
	}
	
	
	@ResponseBody
	@RequestMapping("/delete")
	@RequiresPermissions("permission:delete")
	public MessageObject delete(Long permissionId) {
		
		/*
		 * 删除前要先判断该权限是否有子权限，如果有，不能删除，没有才可以删除
		 * 
		 * 
		 */
		
		PermissionExample example = new PermissionExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentIdEqualTo(permissionId);
		
		List<Permission> children = permissionService.selectByExample(example);
		
		if(children.size() > 0) {
			MessageObject mo = MessageObject.creatMO(0, "该权限还有子权限，不能删除！！");
			return mo;
		}
		
		int row = permissionService.deleteByPrimaryKey(permissionId);
		
		MessageObject mo = MessageObject.creatMO(0, "删除数据失败，请联系管理员！！");
		
		//删除成功
		if(row == 1) {
			mo = MessageObject.creatMO(1, "删除数据成功！！！");
		}
		
		return mo;
	}
	
	
	
	
	/*
	 * 新增或者修改跳转的编辑页面
	 * 
	 */
	
	@RequestMapping("/permissionEdit")
	public String permissionEdit(Model m,Long permissionId) {
		
		
		/*
		 * 如果是修改，有permissionId，要根据id查询出对应的管理员并共享让jsp页面回显
		 */
		if(permissionId != null) {
			System.out.println("AdminController.permissionEdit()");
			Permission permission = permissionService.selectByPrimaryKey(permissionId);
			m.addAttribute("permission", permission);
		}
		
		//查询出所有的父权限：根据业务需求决定，只有菜单权限才能做父权限（type=menu）
		PermissionExample example = new PermissionExample();
		Criteria criteria = example.createCriteria();
		criteria.andTypeEqualTo("menu");
		if(permissionId !=null) {
			//如果当前修改权限是父权限，不能查询自己
			criteria.andPermissionIdNotEqualTo(permissionId);
		}
		
		List<Permission> parents = permissionService.selectByExample(example);
		
		m.addAttribute("parents", parents);
		
		
		return "permissionEdit";
	}
	
	@RequestMapping("/checkPermissionname")
	@ResponseBody
	public boolean checkPermissionname(String permissionname) {
		
		PermissionExample example = new PermissionExample();
		Criteria criteria = example.createCriteria();
		criteria.andNameEqualTo(permissionname);
		
		List<Permission> permissions = permissionService.selectByExample(example );
		
		return permissions.size() == 0 ? true : false;
	}
	
	
	
	
	
}
